package com.molichuxing.gateway.api.services.impl;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.molichuxing.framework.exception.BizException;
import com.molichuxing.framework.result.ResponseResult;
import com.molichuxing.gateway.api.services.TerminalUserApiService;
import com.molichuxing.gateway.bff.user.entity.request.create.IdentityCardCreateVo;
import com.molichuxing.gateway.bff.user.entity.request.create.IdentityCardImgCreateVo;
import com.molichuxing.gateway.bff.user.entity.request.create.UserCreateVo;
import com.molichuxing.gateway.bff.user.entity.request.modify.AccountModifyVo;
import com.molichuxing.gateway.bff.user.entity.request.modify.UserModifyVo;
import com.molichuxing.gateway.bff.user.entity.response.AccountVo;
import com.molichuxing.gateway.bff.user.entity.response.UserLoginVo;
import com.molichuxing.gateway.bff.user.services.TerminalUserBffService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * @author liyg
 * 用户智能终端网关接口
 * 2019-10-17
 */
@Api(tags = "智能终端用户相关接口")
@RestController
@RequestMapping(value = "/terminal")
public class TerminalUserApiServiceImpl extends RootApiService implements TerminalUserApiService{

    private static final Logger logger = LoggerFactory.getLogger(TerminalUserApiServiceImpl.class);
	
	@Resource
	private TerminalUserBffService terminalUserBffService;
	
	/**
	 * 终端身份证识别接口
	 * @param createVo
	 * @return
	 */
	@Override
	@ApiOperation(value = "终端身份证识别接口")
	@PostMapping(value = "/identity")
	public ResponseResult<UserLoginVo> createIdentityCard(@RequestBody IdentityCardCreateVo createVo){
		ResponseResult<UserLoginVo>  result = new ResponseResult<>();
		UserLoginVo vo=new UserLoginVo();
		result.setData(vo);
		try {
			return terminalUserBffService.createIdentityCard(createVo);
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[createIdentityCard]终端身份证识别接口异常", e);
			result.error("终端身份证识别接口异常");
		}
		return result;
	}
	     

	/**
	 * (终端)用户登录注册接口
	 * @param createVo
	 * @param token
	 * @return
	 */
	@Override
	@ApiOperation(value = "(终端)用户登录注册接口")
	@PostMapping(value = "/login")
	public ResponseResult<String> createUser(@RequestBody UserCreateVo createVo,
			@RequestHeader(value = "token", required = true) String token){
		ResponseResult<String>  result = new ResponseResult<>();
		try {
			return terminalUserBffService.createUser(createVo, token);
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[login](终端)用户登录注册接口", e);
			result.error("用户登录接口异常",null);
		}
		return result;
	}
	
	/**
	 * 修改用户信息接口
	 * @param modifyVo
	 * @param token
	 * @return
	 */
	@Override
	@ApiOperation(value = "修改用户信息接口")
	@PostMapping(value = "/modify")
	public ResponseResult<Boolean> modifyUser(@RequestBody UserModifyVo modifyVo,@RequestHeader(value = "token", required = true) String token){
		ResponseResult<Boolean>  result = new ResponseResult<>();
		try {
			Boolean flag = terminalUserBffService.modifyUser(modifyVo,token);
			return result.success(flag);
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[modifyUser]修改用户信息接口异常", e);
			result.error("修改用户信息接口异常",null);
		}
		return result;
	}
	
	/**
	 * 大屏销售顾问登录接口
	 * @param modifyVo
	 * @param token
	 * @return
	 */
	@Override
	@ApiOperation(value = "大屏销售顾问登录接口")
	@PostMapping(value = "/counselor/login")
	public ResponseResult<String> counselorLogin(@RequestBody AccountModifyVo modifyVo){
		ResponseResult<String>  result = new ResponseResult<>();
		try {
			return terminalUserBffService.counselorLogin(modifyVo);
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[counselorLogin]大屏销售顾问登录接口异常", e);
			result.error("大屏销售顾问登录接口异常",null);
		}
		return result;
	}
	
	/**
	 * 获取该门店的销售顾问信息
	 * @param deviceNum
	 * @return
	 */
	@Override
	@ApiOperation(value = " 获取该门店的销售顾问信息接口")
	@GetMapping(value = "/store/account")
	public ResponseResult<List<AccountVo>> getStroeAccount(@RequestHeader(value = "deviceNum", required = true) String deviceNum){
		ResponseResult<List<AccountVo>>  result = new ResponseResult<>();
		try {
			return result.success(terminalUserBffService.getStroeAccount(deviceNum));
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[getStroeAccount] 获取该门店的销售顾问信息接口异常", e);
			result.error(" 获取该门店的销售顾问信息接口异常",null);
		}
		return result;
	}
	
	
	/**
	 * 保存身份证图片接口
	 * @param createVo
	 * @return
	 */
	@Override
	@ApiOperation(value = "保存身份证图片接口")
	@PostMapping(value = "/identity/create")
	public ResponseResult<Boolean> createCardImg(@RequestBody IdentityCardImgCreateVo createVo){
		ResponseResult<Boolean>  result = new ResponseResult<>();
		try {
			 Boolean flag = terminalUserBffService.createCardImg(createVo);
			 if(flag) {
				 return result.success(flag);
			 }
			 result.error(500,"保存身份证图片异常");
		}catch (BizException e) {
			result.error(e.getExceptionCode(),e.getMessage());
		}catch (IllegalArgumentException e) {
			result.error(400,e.getMessage());
		}catch (Exception e) {
			logger.error("[createCardImg]保存身份证图片接口异常", e);
			result.error("保存身份证图片接口异常",null);
		}
		return result;
	}
}
